package javaTodotxt;

import java.io.FileReader;
//import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
//import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;

import javaTodotxt.TodotxtFileIO;

public class TodotxtInterpreter {
	
	public TodotxtFileIO defTodo = new TodotxtFileIO();
	public TodotxtFileIO doneTodo = new TodotxtFileIO();
	public ArrayList<TodotxtFileIO> todos = new ArrayList<TodotxtFileIO>();
	public String currFile = "";
	
	public TodotxtInterpreter() throws IOException {
		this("javaTodoTxt.cfg");
	}
	
	public TodotxtInterpreter(String file) throws IOException  {
		BufferedReader inputStream = null;
		String todoFile;
		String doneFile;
		String todoName;
		String context;
		
        try {
            inputStream = 
                new BufferedReader(new FileReader(file));
            
            if ((todoFile = inputStream.readLine()) == null){
            	throw new Exception("Bad config file");
            }
            if ((todoName = inputStream.readLine()) == null){
            	throw new Exception("Bad config file");
            }
            if ((doneFile = inputStream.readLine()) == null){
            	throw new Exception("Bad config file");
            }
            if ((currFile = inputStream.readLine()) == null){
            	throw new Exception("Bad config file");
            }
            
            
            defTodo = new TodotxtFileIO(todoFile,todoName,"*");
            doneTodo = new TodotxtFileIO(doneFile,"done","*");
            
            while ((todoFile = inputStream.readLine()) != null) {
            	if ((todoName = inputStream.readLine()) == null){
                	throw new Exception("Bad config file");
                }
                if ((context = inputStream.readLine()) == null){
                	throw new Exception("Bad config file");
                }
                todos.add(new TodotxtFileIO(todoFile,todoName,context));
            }
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            System.err.println("Caught IOException: "
                    + e.getMessage());
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
	}
	
	private boolean isInt(String i){
		try{
			Integer.parseInt(i);
			return true;
		}	catch(NumberFormatException nfe){
			return false;
		}
	}
	
	private boolean add(String args) throws Exception{
		String usage = "usage: add \"TODO ITEM\"";
		TodotxtFileIO fileTodo = null;
		if (args.equals("")){
			System.out.println(usage);
		}
		for (int i = 0;i < todos.size();i++){
			if(args.contains("@"+todos.get(i).getContext())){
				fileTodo = todos.get(i);
			}
		}
		if (fileTodo == null){
			fileTodo = defTodo;
		}
		fileTodo.add(args);
		return true;
	}
	private boolean addMulti(String args) throws Exception{
		String usage = "Type Q to quit adding tasks";
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		TodotxtFileIO fileTodo = null;
		String task = "";
		System.out.println(usage);
		if (args.equals("")){
			task = in.readLine();
		}else{
			task = args;
		}
		while(!task.toLowerCase().equals("q")){
			if (task.equals("")){
				System.out.println(usage);
			}else{
				for (int i = 0;i < todos.size();i++){
					if(task.contains("@"+todos.get(i).getContext())){
						fileTodo = todos.get(i);
					}
				}
				if (fileTodo == null){
					fileTodo = defTodo;
				}
				fileTodo.add(task);
			}
			task = in.readLine();
			
		}
		return true;
	}
	private boolean addTo(String args) throws Exception{
		String usage = "usage: addto DEST \"TODO ITEM\"";
		TodotxtFileIO fileTodo = null;
		String file = "";
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.contains(" ")){
			file = args.substring(0, args.indexOf(' '));
			args = args.substring(args.indexOf(' ')+1);
		}else{
			file = args;
			args = "";
		}
		for (int i = 0;i < todos.size();i++){
			if(todos.get(i).getTodoName().equals(file)){
				fileTodo = todos.get(i);
			}
		}
		if(defTodo.getTodoName().equals(file)){
			fileTodo = defTodo;
		}
		if (args.equals("")){
			System.out.println(usage);
		}
		for (int i = 0;i < todos.size();i++){
			if(args.contains("@"+todos.get(i).getContext())){
				fileTodo = todos.get(i);
			}
		}
		if (fileTodo == null){
			fileTodo = defTodo;
		}
		fileTodo.add(args);
		return true;
	}
	private boolean list(String args,PrintWriter out, boolean priority) throws Exception{
		ArrayList<String> tasks = null;
		if (!args.equals("")){
			ArrayList<String> srch = new ArrayList<String>();
			String[] argsArray = args.split(" ");
			for (int i = 0; i < argsArray.length; i++) {
				srch.add(argsArray[i]);
			}
			if (!priority){
				tasks = defTodo.search(srch);
			}else{
				tasks = defTodo.searchPriority(srch);
			}
		}else{
			if (!priority){
				tasks = defTodo.read();
			}else{
				tasks = defTodo.readPriority();
			}
		}
		for (int i = 0;i<tasks.size();i++){
			out.println(tasks.get(i));
		}
		return true;
	}
	private boolean listAll(String args,PrintWriter out, boolean priority) throws Exception{
		ArrayList<String> tasks = new ArrayList<String>();
		if (!args.equals("")){
			ArrayList<String> srch = new ArrayList<String>();
			String[] argsArray = args.split(" ");
			for (int i = 0; i < argsArray.length; i++) {
				srch.add(argsArray[i]);
			}
			if (!priority){
				tasks.add(defTodo.getTodoName());
				tasks.addAll(defTodo.search(srch));
			}else{
				tasks.addAll(defTodo.searchPriority(srch));
			}
			for (int i = 0;i<todos.size();i++){
				if (!priority){
					tasks.add(todos.get(i).getTodoName());
				}
				if (!priority){
					tasks.add(todos.get(i).getTodoName());
					tasks.addAll(todos.get(i).search(srch));
				}else{
					tasks.addAll(todos.get(i).searchPriority(srch));
				}
			}
		}else{
			if (!priority){
				tasks.add(defTodo.getTodoName());
				tasks.addAll(defTodo.read());
			}else{
				tasks.addAll(defTodo.readPriority());
				for (int i = 0;i<tasks.size();i++){
					String tmp = tasks.get(i);
					tmp = tmp.substring(tmp.indexOf(" ")+1)+" ["+defTodo.getTodoName()+" "+tmp.substring(0, tmp.indexOf(" "))+"]";
					tasks.set(i, tmp);
				}
			}
			
			for (int i = 0;i<todos.size();i++){
				if (!priority){
					tasks.add(todos.get(i).getTodoName());
					tasks.addAll(todos.get(i).read());
				}else{
					int start = tasks.size();
					tasks.addAll(todos.get(i).readPriority());
					for (int j = start;j<tasks.size();j++){
						String tmp = tasks.get(j);
						tmp = tmp.substring(tmp.indexOf(" ")+1)+" ["+todos.get(i).getTodoName()+" "+tmp.substring(0, tmp.indexOf(" "))+"]";
						tasks.set(j, tmp);
					}
				}
			}
		}
		if (priority){
			Collections.sort(tasks);
		}
		for (int i = 0;i<tasks.size();i++){
			out.println(tasks.get(i));
		}
		return true;
	}
	private boolean listFile(String args,PrintWriter out, boolean priority) throws Exception{
		ArrayList<String> tasks = null;
		TodotxtFileIO fileTodo = null;
		String file = "";
		if (args.equals("")){
			tasks = new ArrayList<String>();
			tasks.add(defTodo.getTodoName());
			for (int i = 0;i < todos.size();i++){
				tasks.add(todos.get(i).getTodoName());
			}
			tasks.add(doneTodo.getTodoName());
		}else{
			if (args.contains(" ")){
				file = args.substring(0, args.indexOf(' '));
				args = args.substring(args.indexOf(' ')+1);
			}else{
				file = args;
				args = "";
			}
			System.out.println(file);
			for (int i = 0;i < todos.size();i++){
				if(todos.get(i).getTodoName().equals(file)){
					fileTodo = todos.get(i);
				}
			}
			if(doneTodo.getTodoName().equals(file)){
				fileTodo = doneTodo;
			}
			if(defTodo.getTodoName().equals(file)){
				fileTodo = defTodo;
			}
			if (fileTodo == null){
				System.out.println("List not found");
				return true;
			}
			if (!args.equals("")){
				ArrayList<String> srch = new ArrayList<String>();
				String[] argsArray = args.split(" ");
				for (int i = 0; i < argsArray.length; i++) {
					srch.add(argsArray[i]);
				}
				if (!priority){
					tasks = fileTodo.search(srch);
				}else{
					tasks = fileTodo.searchPriority(srch);
				}
			}else{
				if (priority){
					tasks = fileTodo.read();
				}else{
					tasks = fileTodo.readPriority();
				}
			}
		}
		for (int i = 0;i<tasks.size();i++){
			out.println(tasks.get(i));
		}
		return true;
	}
	private boolean listCon(String args,PrintWriter out, boolean priority) throws Exception{//*** Add Pri function
		ArrayList<String> tasks = new ArrayList<String>();
		if (args.equals("")){
			//print error
		}else{
			if (!args.equals("")){
				ArrayList<String> srch = new ArrayList<String>();
				String[] argsArray = args.split(" ");
				for (int i = 0; i < argsArray.length; i++) {
					srch.add("@"+argsArray[i]);
				}
				tasks.add(defTodo.getTodoName());
				tasks.addAll(defTodo.search(srch));
				for (int i = 0;i<todos.size();i++){
					tasks.add(todos.get(i).getTodoName());
					tasks.addAll(todos.get(i).search(srch));
				}
				tasks.add(doneTodo.getTodoName());
				tasks.addAll(doneTodo.search(srch));
			}
		}
		for (int i = 0;i<tasks.size();i++){
			System.out.println(tasks.get(i));
		}
		return true;
	}
	private boolean listProj(String args,PrintWriter out, boolean priority) throws Exception{//*** Add Pri function
		ArrayList<String> tasks = new ArrayList<String>();
		if (args.equals("")){
			//print error
		}else{
			if (!args.equals("")){
				ArrayList<String> srch = new ArrayList<String>();
				String[] argsArray = args.split(" ");
				for (int i = 0; i < argsArray.length; i++) {
					srch.add("+"+argsArray[i]);
				}
				tasks.add(defTodo.getTodoName());
				tasks.addAll(defTodo.search(srch));
				for (int i = 0;i<todos.size();i++){
					tasks.add(todos.get(i).getTodoName());
					tasks.addAll(todos.get(i).search(srch));
				}
				tasks.add(doneTodo.getTodoName());
				tasks.addAll(doneTodo.search(srch));
			}
		}
		for (int i = 0;i<tasks.size();i++){
			out.println(tasks.get(i));
		}
		return true;
	}
	private boolean archive(String args) throws Exception{
		ArrayList<String> arch = defTodo.archive();
		doneTodo.addMultiple(arch);
		for (int i = 0;1<todos.size();i++){
			arch = todos.get(i).archive();
			doneTodo.addMultiple(arch);
		}
		return true;
	}
	private boolean move(String args) throws Exception{
		String usage = "usage: mv ITEM# DEST [SRC]";
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		TodotxtFileIO srcTodo = null;
		TodotxtFileIO destTodo = null;
		String task = "";
		String src = "";
		String dest = "";
		int index = -1;
		String[] splitArgs = null;
		if (args.contains(" ")){
			splitArgs = args.split(" ");
			if(isInt(splitArgs[0])){
				index = Integer.parseInt(splitArgs[0]);
				dest = splitArgs[1];
				for (int i = 0;i < todos.size();i++){
					if(todos.get(i).getTodoName().equals(dest)){
						destTodo = todos.get(i);
					}
				}
				if(defTodo.getTodoName().equals(dest)){
					destTodo = defTodo;
				}
				if (splitArgs.length > 2){
					src = splitArgs[2];
					for (int i = 0;i < todos.size();i++){
						if(todos.get(i).getTodoName().equals(src)){
							srcTodo = todos.get(i);
						}
					}
					if(defTodo.getTodoName().equals(src)){
						srcTodo = defTodo;
					}
				}else{
					srcTodo = defTodo;
				}
				task = srcTodo.readLine(index);
				System.out.println("Move \""+task+"\" from "+srcTodo.getTodoName()+" to "+destTodo.getTodoName()+"? (y/n)");
				if(in.readLine().toLowerCase().equals("y")){
					srcTodo.remove(index);
					destTodo.add(task);
					System.out.println("\""+task+"\" moved from "+srcTodo.getTodoName()+" to "+destTodo.getTodoName()+".");
					return true;
				}else{
					System.out.println("No tasks moved");
					return true;
				}
			}else{
				System.out.println(usage);
			}
			
		}else{
			System.out.println(usage);
			return true;
		}
		
		return true;
	}
	private boolean append(String args) throws Exception{
		String usage = "usage: append ITEM# \"TEXT TO APPEND\"";
		int index = 0;
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.contains(" ")){
			String indexString = args.substring(0, args.indexOf(' ')).trim();
			if (isInt(indexString)){
				index = Integer.parseInt(indexString);
			}else{
				System.out.println(usage);
				return true;
			}
			args = args.substring(args.indexOf(' ')+1);
		}else{
			System.out.println(usage);
			return true;
		}
		if (args.equals("")){
			System.out.println(usage);
		}
		defTodo.append(" "+args,index);
		return true;
	}
	private boolean prepend(String args) throws Exception{
		String usage = "usage: prepend ITEM# \"TEXT TO PREPEND\"";
		int index = 0;
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.contains(" ")){
			String indexString = args.substring(0, args.indexOf(' ')).trim();
			if (isInt(indexString)){
				index = Integer.parseInt(indexString);
			}else{
				System.out.println(usage);
				return true;
			}
			args = args.substring(args.indexOf(' ')+1);
		}else{
			System.out.println(usage);
			return true;
		}
		if (args.equals("")){
			System.out.println(usage);
		}
		defTodo.prepend(args+" ",index);
		return true;
	}
	private boolean pri(String args) throws Exception{
		String usage = "usage: pri ITEM# PRIORITY\nnote: PRIORITY must be anywhere from A to Z.";
		String curr = "";
		int index = 0;
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.contains(" ")){
			String indexString = args.substring(0, args.indexOf(' ')).trim();
			if (isInt(indexString)){
				index = Integer.parseInt(indexString);
			}else{
				System.out.println(usage);
				return true;
			}
			args = args.substring(args.indexOf(' ')+1);
		}else{
			System.out.println(usage);
			return true;
		}
		if (args.equals("") || args.length()>1 ){
			System.out.println(usage);
			return true;
		}
		curr = defTodo.readLine(index);
		if(curr.substring(0,1).equals("(")){
			defTodo.replace("("+args.toUpperCase()+") "+curr.substring(4), index);
		}else{
			defTodo.prepend("("+args.toUpperCase()+") ",index);
		}
		return true;
	}
	private boolean depri(String args) throws Exception{
		String usage = "usage: depri ITEM#[, ITEM#, ITEM#, ...]";
		String[] items;
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.equals("") || !args.contains(",")){
			System.out.println(usage);
			return true;
		}
		items = args.split(",");
		for (int i = 0; i < items.length; i++) {
			String indexString = items[i];
			int index = 0;
			String curr = "";
			if (isInt(indexString.trim())){
				index = Integer.parseInt(indexString.trim());
			}else{
				System.out.println(usage);
				return true;
			}
			curr = defTodo.readLine(index);
			if(curr.substring(0,1).equals("(")){
				defTodo.replace(curr.substring(4), index);
			}
		}
		
		return true;
	}
	private boolean doTask(String args) throws Exception{
		String usage = "usage: do ITEM#[, ITEM#, ITEM#, ...]";
		String[] items;
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.equals("") || (!args.contains(",") && !isInt(args))){
			System.out.println(usage);
			return true;
		}
		items = args.split(",");
		for (int i = 0; i < items.length; i++) {
			String indexString = items[i];
			int index = 0;
			String curr = "";
			if (isInt(indexString.trim())){
				index = Integer.parseInt(indexString.trim());
			}else{
				System.out.println(usage);
				return true;
			}
			curr = defTodo.readLine(index);
			if(curr.substring(0,1).equals("(")){
				defTodo.replace("X "+curr.substring(4), index);
			}else if (!curr.substring(0,1).equals("X")){
				defTodo.replace("X "+curr, index);
			}
		}
		
		return true;
	}
	private boolean del(String args) throws Exception{
		String usage = "usage: $TODO_SH del ITEM# [TERM]";
		int index = 0;
		String term = "";
		String task = "";
		if (args.equals("")){
			System.out.println(usage);
			return true;
		}
		if (args.contains(" ")){
			String indexString = args.substring(0, args.indexOf(' ')).trim();
			term = args.substring(args.indexOf(' ')+1);
			if (isInt(indexString)){
				index = Integer.parseInt(indexString);
			}else{
				System.out.println(usage);
				return true;
			}
			args = args.substring(args.indexOf(' ')+1);
			task = defTodo.readLine(index);
			if (task.contains(term)){
				task = task.replace(term, "");
				defTodo.replace(task, index);
			}else{
				System.out.println("Line "+Integer.toString(index)+" does not contain '"+term+"'");
			}
		}else{
			String indexString = args;
			if (isInt(indexString)){
				index = Integer.parseInt(indexString);
			}else{
				System.out.println(usage);
				return true;
			}
			defTodo.remove(index);
		}
		return true;
	}
	private boolean help(String args) throws Exception{
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		System.out.println(" Actions:\n    add \"THING I NEED TO DO +project @context\"\n    a \"THING I NEED TO DO +project @context\"\n      Adds THING I NEED TO DO to your todo.txt file on its own line.\n      Project and context notation optional.\n      Quotes optional.\n\n    addm \"FIRST THING I NEED TO DO +project1 @context\n    SECOND THING I NEED TO DO +project2 @context\"\n      Adds FIRST THING I NEED TO DO to your todo.txt on its own line and\n      Adds SECOND THING I NEED TO DO to you todo.txt on its own line.\n      Project and context notation optional.\n      Quotes optional.\n\n    addto DEST \"TEXT TO ADD\"\n      Adds a line of text to any file located in the todo.txt directory.\n      For example, addto inbox.txt \"decide about vacation\"");
		if(in.readLine().toLowerCase().equals("q")){
			return true;
		}
		System.out.println("    append ITEM# \"TEXT TO APPEND\"\n    app ITEM# \"TEXT TO APPEND\"\n      Adds TEXT TO APPEND to the end of the task on line ITEM#.\n      Quotes optional.\n\n    archive\n      Moves all done tasks from todo.txt to done.txt and removes blank lines.\n\n    command [ACTIONS]\n      Runs the remaining arguments using only todo.sh builtins.\n      Will not call any .todo.actions.d scripts.\n\n    del ITEM# [TERM]\n    rm ITEM# [TERM]\n      Deletes the task on line ITEM# in todo.txt.\n      If TERM specified, deletes only TERM from the task.");
		if(in.readLine().toLowerCase().equals("q")){
			return true;
		}
		System.out.println("    depri ITEM#[, ITEM#, ITEM#, ...]\n    dp ITEM#[, ITEM#, ITEM#, ...]\n      Deprioritizes (removes the priority) from the task(s)\n      on line ITEM# in todo.txt.\n\n    do ITEM#[, ITEM#, ITEM#, ...]\n      Marks task(s) on line ITEM# as done in todo.txt.\n\n    help\n      Display this help message.\n\n    list [TERM...]\n    ls [TERM...]\n      Displays all tasks that contain TERM(s) sorted by priority with line\n      numbers.  If no TERM specified, lists entire todo.txt.");
		if(in.readLine().toLowerCase().equals("q")){
			return true;
		}
		System.out.println("    listall [TERM...]\n    lsa [TERM...]\n      Displays all the lines in todo.txt AND done.txt that contain TERM(s)\n      sorted by priority with line  numbers.  If no TERM specified, lists\n      entire todo.txt AND done.txt concatenated and sorted.\n\n    listcon\n    lsc\n      Lists all the task contexts that start with the @ sign in todo.txt.\n\n    listfile SRC [TERM...]\n    lf SRC [TERM...]\n      Displays all the lines in SRC file located in the todo.txt directory,\n      sorted by priority with line  numbers.  If TERM specified, lists\n      all lines that contain TERM in SRC file.\n\n    listpri [PRIORITY]\n    lsp [PRIORITY]\n      Displays all tasks prioritized PRIORITY.\n      If no PRIORITY specified, lists all prioritized tasks.");
		if(in.readLine().toLowerCase().equals("q")){
			return true;
		}
		System.out.println("    listproj\n    lsprj\n      Lists all the projects that start with the + sign in todo.txt.\n\n    move ITEM# DEST [SRC]\n    mv ITEM# DEST [SRC]\n      Moves a line from source text file (SRC) to destination text file (DEST).\n      Both source and destination file must be located in the directory defined\n      in the configuration directory.  When SRC is not defined\n      it\'s by default todo.txt.\n\n    prepend ITEM# \"TEXT TO PREPEND\"\n    prep ITEM# \"TEXT TO PREPEND\"\n      Adds TEXT TO PREPEND to the beginning of the task on line ITEM#.\n      Quotes optional.\n\n    pri ITEM# PRIORITY\n    p ITEM# PRIORITY\n      Adds PRIORITY to task on line ITEM#.  If the task is already\n      prioritized, replaces current priority with new PRIORITY.\n      PRIORITY must be an uppercase letter between A and Z.");
		if(in.readLine().toLowerCase().equals("q")){
			return true;
		}
		System.out.println("    replace ITEM# \"UPDATED TODO\"\n      Replaces task on line ITEM# with UPDATED TODO.\n\n    report\n      Adds the number of open tasks and done tasks to report.txt.");
		return true;
	}
	
	public boolean interpret(String command) throws Exception{
		String comm = "";
		String args = "";
		boolean success = true;
		boolean fileOut = false;
		boolean priority = false;
		PrintWriter consoleOut = new PrintWriter(System.out,true);
		PrintWriter out = consoleOut;
		if (command.contains(" ")){
			comm = command.substring(0, command.indexOf(' '));
			args = command.substring(command.indexOf(' ')+1);
		}else{
			comm = command;
		}
		comm = comm.toLowerCase();
		while (!args.equals("") && args.substring(0, 1).toLowerCase().equals("-")){
			String mod = args.substring(1, 2).toLowerCase();
			if (args.contains(" ")){
				args = args.substring(3);
			}else{
				args = "";
			}
			if(mod.equals("c")){
				out = new PrintWriter(new FileWriter(currFile));
				fileOut = true;
			}
			if(mod.equals("p")){
				priority = true;
			}
		}
		if (comm.equals("add") || comm.equals("a")){
			success = add(args);
		}else if (comm.equals("addm")){
			success = addMulti(args);
		}else if (comm.equals("addto")){
			success = addTo(args);
		}else if (comm.equals("append") || comm.equals("app")){
			success = append(args);
		}else if (comm.equals("prepend") || comm.equals("prep")){
			success = prepend(args);
		}else if (comm.equals("pri") || comm.equals("p")){
			success = pri(args);
		}else if (comm.equals("depri") || comm.equals("dp")){
			success = depri(args);
		}else if (comm.equals("do")){
			success = doTask(args);
		}else if (comm.equals("del") || comm.equals("rm")){
			success = del(args);
		}else if (comm.equals("move") || comm.equals("mv")){
			success = move(args);
		}else if (comm.equals("list") || comm.equals("ls")){
			success = list(args,out,priority);
		}else if (comm.equals("listall") || comm.equals("lsa")){
			success = listAll(args,out,priority);
		}else if (comm.equals("listfile") || comm.equals("lf")){
			success = listFile(args,out,priority);
		}else if (comm.equals("listcon") || comm.equals("lsc")){
			success = listCon(args,out,priority);
		}else if (comm.equals("listproj") || comm.equals("lsprj")){
			success = listProj(args,out,priority);
		}else if (comm.equals("archive")){
			success = archive(args);
		}else if (comm.equals("help")){
			success = help(args);
		}else if (comm.equals("quit") || comm.equals("exit") || comm.equals("q")){
			success = false;
		}else{
			System.out.println("Unrecognized command "+comm);
			success = true;
		}
		if (fileOut) {
			out.close();
        }
		return success;
	}

}
